package com.xingchen.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.mylibs.R;
import com.xingchen.widget.OnWheelChangedListener;
import com.xingchen.widget.OnWheelScrollListener;
import com.xingchen.widget.WheelView;

import java.util.ArrayList;
import java.util.Calendar;


/**
 * 日期选择对话框
 *
 * @author ywl
 */
public class ChangeDateDialog extends Dialog {

    private Context context;
    private WheelView wvMonth;

    private ArrayList<String> arry_months = new ArrayList<String>();
    private CalendarTextAdapter mMonthAdapter;

    private int month = 36;

    private int currentMonth = 1;

    private int maxTextSize = 28;
    private int midTextSize = 18;
    private int minTextSize = 14;

    private boolean issetdata = false;

    private String selectMonth;

    private OnBirthListener onBirthListener;

    public ChangeDateDialog(Context context) {
        super(context, R.style.ShareDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_myinfo_changebirth);
        wvMonth = (WheelView) findViewById(R.id.wv_birth_month);
        if (!issetdata) {
            initData();
        }
        initMonths(month);
        mMonthAdapter = new CalendarTextAdapter(context, arry_months, currentMonth, maxTextSize, minTextSize);
        wvMonth.setVisibleItems(5);
        wvMonth.setViewAdapter(mMonthAdapter);
        wvMonth.setCurrentItem(currentMonth);

        wvMonth.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem());
                selectMonth = currentText;
                setTextviewSize(currentText, mMonthAdapter);
            }
        });

        wvMonth.addScrollingListener(new OnWheelScrollListener() {

            @Override
            public void onScrollingStarted(WheelView wheel) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                // TODO Auto-generated method stub
                String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem());
                setTextviewSize(currentText, mMonthAdapter);
            }
        });
    }


    public void initMonths(int months) {
        arry_months.clear();
        for (int i = 1; i <= months; i++) {
            arry_months.add(i + "");
        }
    }

    public void setDateListener(OnBirthListener onBirthListener) {
        this.onBirthListener = onBirthListener;
    }

    public interface OnBirthListener {
        public void onClick(String year, String month, String day, String hour, String minute);
    }

    /**
     * 设置字体大小
     *
     * @param curriteItemText
     * @param adapter
     */
    public void setTextviewSize(String curriteItemText, CalendarTextAdapter adapter) {
        ArrayList<View> arrayList = adapter.getTestViews();
        int size = arrayList.size();
        int currentItemInde = Integer.parseInt(curriteItemText);
        int temp;
        String currentText;
        for (int i = 0; i < size; i++) {
            TextView textvew = (TextView) arrayList.get(i);
            currentText = textvew.getText().toString();
            temp = Integer.parseInt(currentText);
            if (curriteItemText.equals(currentText)) {
                textvew.setTextSize(maxTextSize);
                textvew.setTextColor(Color.parseColor("#ffffffff"));//星辰添加
            } else if (Math.abs(currentItemInde - temp) == 1) {
                textvew.setTextSize(midTextSize);
                textvew.setTextColor(Color.parseColor("#cfffffff"));//星辰添加
            } else {
                textvew.setTextSize(minTextSize);
                textvew.setTextColor(Color.parseColor("#8fffffff"));//星辰添加
            }
        }
    }

    public int getMonth() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MONTH) + 1;
    }

    public void initData() {
        setDate(getMonth());
    }

    /**
     * 设置年月日
     *
     * @param month
     */
    public void setDate(int month) {
        this.currentMonth = month;
    }
}